from sqlalchemy import Column, BigInteger, String, SmallInteger, Date, DateTime, Boolean, Index
from app.config.database import Base
from datetime import datetime, date
from pydantic import BaseModel, ConfigDict
from typing import Optional

class AcademicYear(Base):
    __tablename__ = "edu_academic_year"
    
    id = Column(BigInteger, primary_key=True, autoincrement=True, comment="学年ID")
    year_code = Column(String(20), nullable=False, comment="学年编码（如：2025-2026）")
    year_name = Column(String(50), nullable=False, comment="学年名称")
    start_date = Column(Date, nullable=False, comment="开始日期")
    end_date = Column(Date, nullable=False, comment="结束日期")
    semester = Column(SmallInteger, nullable=False, comment="学期：1-秋季学期 2-春季学期")
    status = Column(SmallInteger, nullable=False, default=1, comment="状态：0-停用 1-启用")
    is_current = Column(Boolean, nullable=False, default=False, comment="是否当前学年：0-否 1-是")
    create_time = Column(DateTime, default=datetime.utcnow, nullable=False, comment="创建时间")
    update_time = Column(DateTime, default=datetime.utcnow, onupdate=datetime.utcnow, nullable=False, comment="更新时间")
    
    # 索引
    __table_args__ = (
        Index('uk_year_semester', 'year_code', 'semester'),
    )

class AcademicYearInDB(BaseModel):
    id: int
    year_code: str
    year_name: str
    start_date: date
    end_date: date
    semester: int
    status: int = 1
    is_current: bool = False
    create_time: Optional[datetime] = None
    update_time: Optional[datetime] = None

    model_config = ConfigDict(from_attributes=True)